<?php

/**
 * This is the model class for table "tbl_gebruiker".
 *
 * The followings are the available columns in table 'tbl_gebruiker':
 * @property integer $id
 * @property string $voornaam
 * @property string $achternaam
 * @property string $email
 * @property string $wachtwoord
 * @property string $resifnummer
 * @property string $registratiedatum
 * @property string $specialisatie
 * @property string $geboorte_datum
 * @property integer $gemeente
 * @property string $telefoon
 * @property string $school
 * @property integer $is_admin
 *
 * The followings are the available model relations:
 * @property TblOnderzoek[] $tblOnderzoeks
 */
class Gebruiker extends ExtendedActiveRecord{

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Gebruiker the static model class
     */
    
    
    
    public $Gebruiker_role;

    public static function model($className=__CLASS__){
        return parent::model($className);
    }


    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
            return 'tbl_gebruiker';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
            // NOTE: you should only define rules for those attributes that
            // will receive user inputs.
            return array(
                    array('voornaam, achternaam, email, wachtwoord, resifnummer, registratiedatum', 'required'),
                    array('gemeente, is_admin', 'numerical', 'integerOnly'=>true),
                    array('voornaam, achternaam, email, wachtwoord, resifnummer, telefoon, school', 'length', 'max'=>255),
                    array('specialisatie, geboorte_datum', 'safe'),
                    // The following rule is used by search().
                    // Please remove those attributes that should not be searched.
                    array('id, voornaam, achternaam, email, wachtwoord, resifnummer, registratiedatum, specialisatie, geboorte_datum, gemeente, telefoon, school, is_admin', 'safe', 'on'=>'search'),
            );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
            // NOTE: you may need to adjust the relation name and the related
            // class name for the relations automatically generated below.
            return array(
                    'tblOnderzoeks' => array(self::HAS_MANY, 'TblOnderzoek', 'maker_id'),
            );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
            return array(
                    'id' => 'ID',
                    'voornaam' => 'Voornaam',
                    'achternaam' => 'Achternaam',
                    'email' => 'Email',
                    'wachtwoord' => 'Wachtwoord',
                    'resifnummer' => 'Resifnummer',
                    'registratiedatum' => 'Registratiedatum',
                    'specialisatie' => 'Specialisatie',
                    'geboorte_datum' => 'Geboorte Datum',
                    'gemeente' => 'Gemeente',
                    'telefoon' => 'Telefoon',
                    'school' => 'School',
                    'is_admin' => 'Is Admin',
            );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
            // Warning: Please modify the following code to remove attributes that
            // should not be searched.

            $criteria=new CDbCriteria;

            $criteria->compare('id',$this->id);
            $criteria->compare('voornaam',$this->voornaam,true);
            $criteria->compare('achternaam',$this->achternaam,true);
            $criteria->compare('email',$this->email,true);
            $criteria->compare('wachtwoord',$this->wachtwoord,true);
            $criteria->compare('resifnummer',$this->resifnummer,true);
            $criteria->compare('registratiedatum',$this->registratiedatum,true);
            $criteria->compare('specialisatie',$this->specialisatie,true);
            $criteria->compare('geboorte_datum',$this->geboorte_datum,true);
            $criteria->compare('gemeente',$this->gemeente);
            $criteria->compare('telefoon',$this->telefoon,true);
            $criteria->compare('school',$this->school,true);
            $criteria->compare('is_admin',$this->is_admin);

            return new CActiveDataProvider($this, array(
                    'criteria'=>$criteria,
            ));
    }
    
    public function getGebruikerById($id){
         return Gebruiker::model()->findByPk($id);
    }
    
    
    protected function afterValidate(){
                    parent::afterValidate();
                    $this->password = $this->encrypt($this->password);
    }

    protected function encrypt($value){
        return md5($value);
    }
    
    public function setNewRole($id){
        $auth = Yii::app()->authManager;
        $cur_role = $auth->getRoles($id);
        if(!isset($cur_role) or empty($cur_role)){ // heeft hij al een rol?
            //$roles = $auth->getRoles();
            //$Gebruikers_role = ($this->is_admin) ? $roles['beheerder'] : $roles['hio'];
            $new_role = ($this->is_admin) ? 'beheerder' : 'hio';
            $auth->assign($new_role,$id); 
            $this->Gebruiker_role = $new_role;       
        }       
        $this->Gebruiker_role = $cur_role; //wordt nu ook daar bijgehouden niet echt zeker of nodig   
        return $this->Gebruiker_role;
    }
}